package com.lej.core.widget.customview

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.TextureView
import android.view.View
import android.view.ViewOutlineProvider
import kotlin.math.max
import kotlin.math.min

class RoundTextureView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : TextureView(context, attrs, defStyleAttr) {

    init {
        outlineProvider = object : ViewOutlineProvider() {
            override fun getOutline(view: View, outline: Outline) {
                val size = min(view.measuredHeight, view.measuredWidth)
                val offset: Int = max((view.measuredWidth - view.measuredHeight) / 2, 0)
                val rect = Rect(offset, 0, offset + size, size)
                outline.setRoundRect(rect, size / 2.0f)
            }
        }
        clipToOutline = true
    }
}